package top.codedance.iotp.tools.earlyWarning.ui;

import top.codedance.iotp.tools.earlyWarning.EarlyWarningApplication;

import java.awt.AWTException;
import java.awt.Frame;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
import javax.swing.*;

/**
 *
 * @author Lenovo
 */
public class SettingsFrame extends javax.swing.JFrame {

    /**
     * Creates new form SettingsFrame
     */
    public SettingsFrame() {
        initComponents();
        URL url = Thread.currentThread().getContextClassLoader().getResource("logo.gif");
        ImageIcon icon = new ImageIcon(url);
        setIconImage(icon.getImage());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null); 
        setSize(500, 150);
        init();
    }

    public void setWebsocketUrl(String value){
        urlJT.setText(value);
    }

    public void setHttpapi(String httpApi){jtf_httpapi.setText(httpApi);}
    
    private void init(){
        setWebsocketUrl(EarlyWarningApplication.websocketUrl);
        //设置软件在托盘上显示的图标
        Toolkit tk = Toolkit.getDefaultToolkit();
        Image img = tk.getImage(EarlyWarningApplication.logoPath);
        SystemTray systemTray = SystemTray.getSystemTray(); //获得系统托盘的实例
        TrayIcon trayIcon = null;

        try {
            trayIcon = new TrayIcon(img, "油站系统报警检测器");
            systemTray.add(trayIcon);
            this.setIconImage(img);
            trayIcon.setImageAutoSize(true);
        } catch (AWTException e2) {
            e2.printStackTrace();
        }
        
        //窗口最小化时软件dispose
        this.addWindowListener(new WindowAdapter() {
            //图标化窗口时调用事件
            public void windowIconified(WindowEvent e) {
                dispose(); //窗口最小化时dispose该窗口
            }
        });

        //双击托盘图标，软件正常显示
        trayIcon.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1) //双击托盘窗口再现
                    //置此 frame 的状态。该状态表示为逐位掩码。
                    setExtendedState(Frame.NORMAL); //正常化状态
                setVisible(true);
            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        urlJT = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        runJb = new javax.swing.JButton();
        jb_logout = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jtf_httpapi = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("数据地址：");

        jButton1.setText("保存");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        urlJT.setToolTipText("Websocket全路径");

        jButton2.setText("关闭应用");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        runJb.setText("停止报警");
        runJb.setActionCommand("");
        runJb.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runJbActionPerformed(evt);
            }
        });

        jb_logout.setText("登出");
        jb_logout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_logoutActionPerformed(evt);
            }
        });

        jLabel2.setText("登陆地址：");
        jLabel2.setToolTipText("");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 145, Short.MAX_VALUE)
                        .addComponent(jb_logout)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(runJb)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtf_httpapi))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(urlJT)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jButton1)
                    .addComponent(urlJT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtf_httpapi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(runJb)
                    .addComponent(jb_logout))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        EarlyWarningApplication.websocketUrl = urlJT.getText();
        if(!EarlyWarningApplication.websocketUrl.isEmpty()){
            try{
                EarlyWarningApplication.saveConfig();
                JOptionPane.showMessageDialog(rootPane, "保存成功,需要重启才能生效，软件即将关闭，请手动启动程序。");
                System.exit(0);
            }catch(Exception e){
                JOptionPane.showMessageDialog(rootPane, "保存失败");
            }
        }else{
                JOptionPane.showMessageDialog(rootPane, "地址不能为空");
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void runJbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runJbActionPerformed
        EarlyWarningApplication.isStopWarning = true;
    }//GEN-LAST:event_runJbActionPerformed

    private void jb_logoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_logoutActionPerformed
        EarlyWarningApplication.logout();
    }//GEN-LAST:event_jb_logoutActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton jb_logout;
    private javax.swing.JTextField jtf_httpapi;
    private javax.swing.JButton runJb;
    private javax.swing.JTextField urlJT;
    // End of variables declaration//GEN-END:variables
}
